import { WEBSOCKETS, toSuccess } from "@sker/core";
import { useData } from "@sker/runtime";
import { container } from "tsyringe";
import { z } from "zod";

export function downloadFromUrl() {
    const input = z.object({
        path: z.string(),
        url: z.string()
    });
    const body = input.parse(useData())
    const sockets = container.resolve(WEBSOCKETS)
    sockets.forEach(socket => {
        socket.send(JSON.stringify({ type: 'DOWNLOAD_FROM_URL', payload: { url: body.url, path: body.path } }))
    })
    return toSuccess(true)
}


export function downloadFromCode() {
    const input = z.object({
        path: z.string(),
        code: z.string()
    });
    const body = input.parse(useData())
    const sockets = container.resolve(WEBSOCKETS)
    sockets.forEach(socket => {
        socket.send(JSON.stringify({ type: 'DOWNLOAD_FROM_CODE', payload: { code: body.code, path: body.path } }))
    })
    return toSuccess(true)
}

export function uploadFileFromLocal() {
    const input = z.object({
        path: z.string(),
    });
    const body = input.parse(useData())
    const sockets = container.resolve(WEBSOCKETS)
    sockets.forEach(socket => {
        socket.send(JSON.stringify({ type: 'UPLOAD_FILE_FROM_LOCAL', payload: { path: body.path } }))
    })
    return toSuccess(true)
}

